/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年8月5日
 */
package com.massyframework.beanskin.runtime.framework.installing.fragment;

import java.util.Comparator;

import com.massyframework.beanskin.RequireLibraryDescriptor;
import com.massyframework.beanskin.fragment.FragmentBuilder;

/**
 * 片段构建器比较器
 * @author huangkh
 *
 */
public class FragmentBuilderComparator implements Comparator<FragmentBuilder> {

	/**
	 * 
	 */
	public FragmentBuilderComparator() {

	}

	@Override
	public int compare(FragmentBuilder o1, FragmentBuilder o2) {
		RequireLibraryDescriptor require1 =
				o1.findByFilter( 
					desc -> 
						desc.getGroupId().equals(o2.getModuleIdentifier().getGroupId()) &&
						desc.getModuleId().equals(o2.getModuleIdentifier().getModuleId()) && 
						desc.getVersionRange().includes(o2.getModuleIdentifier().getVersion())
				);
		
		RequireLibraryDescriptor require2 =
				o2.findByFilter(
					desc -> 
						desc.getGroupId().equals(o1.getModuleIdentifier().getGroupId()) &&
						desc.getModuleId().equals(o1.getModuleIdentifier().getModuleId()) && 
						desc.getVersionRange().includes(o1.getModuleIdentifier().getVersion())
				);
		
		if (require1 != null && require2 != null) {
			throw new RuntimeException(
					o1.getModuleIdentifier().toIdentifier() + " and " +
					o2.getModuleIdentifier().toIdentifier() + " is interdependence.");
		}
		if (require1 != null) {
			return -1;
		}
		
		if (require2 != null) {
			return 1;
		}
		
		return 0;
	}

	
}
